<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/17 0017
 * Time: 上午 10:44
 */
namespace app\auth\controller;
use app\common\api\auth\IRole;
use app\auth\validate\Vali_role;
use app\auth\validate\ValidateFun;
use think\Db;

class Role implements IRole {
    private static $config = [];
    /**
     * 初始化配置
     * @param array $config
     */
    public function __construct(array $config = [])
    {
        self::$config = array_filter_full(array_merge(self::$config,$config));
    }

    /**
     * 添加角色
     * @return mixed
     */
    public function role_add(){
        $data = self::$config;
        unset($data['role_id']);
        #验证参数
        $vali = Vali_role::add();
        $validate = new ValidateFun($vali['rule'],$vali['message']);
        if (!$validate->check($data)){
            $err = $validate->getError();
            return ['code'=>$err,'msg'=>config("msg.$err")];
        }
        #找出登录这信息
        $info = new \app\auth\model\Role();
        $res = $info->role_add($data);

        #返回结果
        if ($res>0){
            return ['code'=>200,'msg'=>config('msg.200'),'data'=>$res];
        }
        return ['code'=>10006,'msg'=>config('msg.10006')];

    }

    /**
     * 更改用户角色
     * @return mixed
     */
    public function user_role_update(){
        $data = self::$config;
        #验证参数
        $vali = Vali_role::user_update();
        $validate = new ValidateFun($vali['rule'],$vali['message']);
        if (!$validate->check($data)){
            $err = $validate->getError();
            return ['code'=>$err,'msg'=>config("msg.$err")];
        }

        #修改数据
        $update = new \app\auth\model\Role();
        $res = $update->user_role_update($data);

        #返回结果
        if ($res>0){
            return ['code'=>200,'msg'=>config('msg.200')];
        }
        return ['code'=>10025,'msg'=>config('msg.10025')];#数据未更新

    }

    /**
     * 更改/删除 角色信息
     * @return mixed
     */
    public function role_update(){
        $data = self::$config;

        #验证参数
        if(isset($data['del'])){
            $vali = Vali_role::role_del();
        }else{
            $vali = Vali_role::update();
        }
        $validate = new ValidateFun($vali['rule'],$vali['message']);
        if (!$validate->check($data)){
            $err = $validate->getError();
            return ['code'=>$err,'msg'=>config("msg.$err")];
        }

        #修改数据
        $update = new \app\auth\model\Role();
        if(isset($data['del'])){
            $res = $update ->role_del($data);
            if ($res>0){
                return ['code'=>200,'msg'=>config('msg.200')];
            }
            return ['code'=>10007,'msg'=>config('msg.10007')];

        }else{
            $res = $update->role_update($data);
            #返回结果
            return $res;
        }
    }

    /**
     * 角色列表
     * @return mixed
     */
    public function role_list(){
        $default =[
            'curr' =>1,
            'limits'=>10,
        ];
        $data = array_filter_full(array_merge($default,self::$config));
        #获取列表
        $list = new \app\auth\model\Role();
        $result =$list->role_list($data);
        #返回结果
        return ['code'=>200,'msg'=>config('msg.200'),'data'=>$result];
    }
}